package com.zt.dev.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.zt.dev.constant.RelationStatus;
import com.zt.dev.system.dto.TransformBeans;
import com.zt.dev.system.mapper.SystemRelationMapper;
import com.zt.dev.system.mapper.SystemStructureMapper;
import com.zt.dev.system.pojo.SystemRelation;
import com.zt.dev.system.pojo.SystemStructure;
import com.zt.dev.system.service.SystemRelationService;
import com.zt.dev.system.vo.ReceiveRelationVo;
import com.zt.dev.system.vo.SystemStructureVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author 30378
 */
@Service
public class SystemRelationServiceImpl implements SystemRelationService {
    private final SystemStructureMapper structureMapper;
    private final SystemRelationMapper relationMapper;

    private final TransformBeans transformBeans;

    public SystemRelationServiceImpl(SystemStructureMapper structureMapper, SystemRelationMapper relationMapper, TransformBeans transformBeans) {
        this.structureMapper = structureMapper;
        this.relationMapper = relationMapper;
        this.transformBeans = transformBeans;
    }

    @Override
    public int addSysRelation(SystemStructureVo sys, JSONArray structureList) {
        if (sys.getId() != null) {
            // 跟之前体系增加关联,返回的为 体系（任务的）Id
            int id = updateSystemStructure(sys);
            // 保存之间的关系
            return saveSysRelation(structureList, id);
        }
        return 0;
    }

    @Override
    public int delSysRelation(Integer id) {
        SystemRelation relation = relationMapper.selectById(id);
        if (relation != null) {
            return relationMapper.delSysRelation(id);
        }
        return 0;
    }

    @Override
    public int updateSysRelation(ReceiveRelationVo relation) {
        SystemRelation systemRelation = new SystemRelation();
        transformBeans.receiveRelationVoTransformRelation(relation,relation.getId());
        return relationMapper.updateById(systemRelation);
    }

    @Override
    public SystemRelation findSysRelationById(Integer id) {
        return relationMapper.selectById(id);
    }

    @Override
    public int lockSysRelationById(Integer id) {
        return relationMapper.lockSysRelationById(id, RelationStatus.IN_LOCK.getStatus());
    }

    /**
     * 保存系统关系
     **/
    private int saveSysRelation(JSONArray structureList, int id) {
        ArrayList<ReceiveRelationVo> list = (ArrayList<ReceiveRelationVo>) structureList.toJavaList(ReceiveRelationVo.class);
        int count = 0;
        for (int i = 1; i < list.size(); i++) {
            SystemRelation relation = transformBeans.receiveRelationVoTransformRelation(list.get(i), id);
            count = count + relationMapper.insert(relation);
        }
        return count;
    }

    /**
     * 更新体系结构，使其和体系关系的根节点相关联
     * 返回其体系的Id
     **/
    private int updateSystemStructure(SystemStructureVo sys) {
        SystemStructure s = structureMapper.selectById(sys.getId());
        s.setStructureId(sys.getId());
        structureMapper.updateById(s);
        return s.getId();
    }
}
